<template>
  <a-modal
    :title="modelTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="昵称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name', {rules: [{required: true, min: 1, message: '用户昵称不能为空'}]}]"
            placeholder="请输入用户昵称" />
        </a-form-item>
        <a-form-item
          label="登录邮箱"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['email', {rules: [{required: true, type: 'email', message: '请输入合法的邮箱'}]}]"
            :disabled="userInfo.id != 0"
            placeholder="请输入用户邮箱" />
        </a-form-item>
        <a-form-item
          label="密码等级"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-rate v-model="userInfo.password_level" :tooltips="desc" disabled>
            <a-icon slot="character" type="rocket" />
          </a-rate>
          <span class="ant-rate-text">{{ desc[userInfo.password_level - 1] }}</span>
        </a-form-item>
        <a-form-item
          label="密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['password', {rules: [{required: userInfo.id == 0, min: 1, message: '请输入新用户密码'}]}]"
            @change="handlePasswordNewBlur"
            placeholder="请输入密码" />
        </a-form-item>
        <a-form-item
          label="用户头像"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <uploader-single-image v-decorator="['avatar', {rules: [{required: true, min:1, message: '请上传用户头像'}]}]" />
        </a-form-item>
        <a-form-item
          label="关联角色"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-tree-select
            :treeData="roleList"
            v-decorator="['role_ids', {rules: [{required: true, message: '请选择用户角色'}]}]"
            treeCheckable
            :treeCheckStrictly="true"
            :showCheckedStrategy="SHOW_ALL"
            searchPlaceholder="请选择角色" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import UploaderSingleImage from '@/components/Uploader/UploaderSingleImage.vue'
import { TreeSelect } from 'ant-design-vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import storage from 'store'
import { getPasswordSecurityLevel } from '@/utils/util'
import { saveData } from '@/api/system/sysuser'
import { pick } from 'lodash'
const SHOW_ALL = TreeSelect.SHOW_ALL
export default {
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      uploadLoading: false,
      uploadImgUrl: '',
      SHOW_ALL,
      userInfo: {},
      form: this.$form.createForm(this),
      userToken: storage.get(ACCESS_TOKEN),
      desc: ['糟糕', '简单', '一般', '良好', '完美']
    }
  },
  components: {
    UploaderSingleImage
  },
  computed: {
    modelTitle: function () {
      if (this.id === 0) {
        return '新建用户'
      } else {
        return '编辑用户'
      }
    }
  },
  props: {
    roleList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    handlePasswordNewBlur (e) {
      const value = e.target.value
      this.userInfo.password_level = getPasswordSecurityLevel(value) + 1
    },
    add () {
      this.visible = true
      this.userInfo = { id: 0, avatar: '' }
      const { form: { resetFields } } = this
      this.$nextTick(() => {
        resetFields()
      })
    },
    edit (record) {
      this.visible = true
      this.userInfo = record
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['name', 'email', 'avatar', 'role_ids']))
      })
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          saveData(Object.assign(this.userInfo, values)).then(res => {
            this.confirmLoading = false
            if (res.code === 0) {
              this.visible = false
              this.$message.success(res.message)
              this.$emit('ok', values)
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>
